package com.ebelter.myehc.ui.fragments.trends;



import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.models.bluetooth.products.bpm.BpmManager;

import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.CommonUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.TimeUtils;

import com.ebelter.btcomlib.views.ChartView3;
import com.ebelter.btcomlib.views.CombinView7;
import com.ebelter.btcomlib.views.CombinView8;
import com.ebelter.myehc.R;

import com.ebelter.myehc.models.http.response.device_resports.BP_R;
import com.ebelter.myehc.utils.EHCCommonUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ebelter.btcomlib.bases.activity.BaseActivity.getString_;

/**
 * 趋势血压
 */

public class Trend_XueYa_Fragment extends BaseFragment {
    public static final String TAG = "Trend_XueYa_Fragment";


    private static final int[] levelColors = {0xff18d5e5, 0xff14e34e, 0xffffcc00, 0xfff755ed, 0xffff9000, 0xfffc3838, 0xffaa0c0c};

    CombinView7 xy_cb7;
    CombinView8 shsy_cb8;
    CombinView8 shzhy_cb8;
    CombinView8 maibo_cb8;


    ChartView3 trend_xy_ssy_ch3;
    ChartView3 trend_xy_shzhy_ch3;
    ChartView3 trend_xy_maibo_ch3;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_trend_xueya;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    private void FV() {
        xy_cb7 = mRootView.findViewById(R.id.xy_cb7);
        shsy_cb8 = mRootView.findViewById(R.id.xy_shsy_cb8);
        shzhy_cb8 = mRootView.findViewById(R.id.xy_shzhy_cb8);
        maibo_cb8 = mRootView.findViewById(R.id.xy_maibo_cb8);


        if (AppUtils.en==AppUtils.getLocalStr()){
            xy_cb7.setTitleSizes(13);
            shsy_cb8.setTitleSizes(14);
            shzhy_cb8.setTitleSizes(14);
            maibo_cb8.setTitleSizes(14);
        }



        trend_xy_ssy_ch3 = mRootView.findViewById(R.id.trend_xy_ssy_chartview3);
        trend_xy_shzhy_ch3 = mRootView.findViewById(R.id.trend_xy_shzhy_chartview3);
        trend_xy_maibo_ch3 = mRootView.findViewById(R.id.trend_xy_maibo_chartview3);
        trend_xy_ssy_ch3.warringTem = 1000;
        trend_xy_shzhy_ch3.warringTem = 1000;
        trend_xy_maibo_ch3.warringTem = 1000;
        trend_xy_ssy_ch3.yOriStart = 0;
        trend_xy_shzhy_ch3.yOriStart = 0;
        trend_xy_maibo_ch3.yOriStart = 0;

        trend_xy_ssy_ch3.displayInt = true;
        trend_xy_shzhy_ch3.displayInt = true;
        trend_xy_maibo_ch3.displayInt = true;
    }


    //dateType:日期类型 1:年 2:月 3周: 4:天
    //measureType:1:血糖 2:血压 3:血氧 4:体温 5:尿酸 6:总胆固醇 7:人体成份
    public void setReportData(BP_R reportData) {
        if (reportData == null || reportData.resultCode != 1 || reportData.resultData == null) {
            return;
        }


        //y轴坐标对应的数据
        List<Float> xyYValue = new ArrayList<>();
        for (int i = 0; i < 5; i++) {//Y轴的刻度值
            xyYValue.add((float) (i * 60));
        }
        //x轴坐标对应的数据
        List<String> oneXValue = new ArrayList<>();
        List<String> disXValue = new ArrayList<>();
        List<Integer> xPointColor = new ArrayList<>();
        List<Integer> xPointColor2 = new ArrayList<>();
        Map<String, Float> ssyValue = new HashMap<>();
        Map<String, Float> shzhyValue = new HashMap<>();
        Map<String, Float> maiboValue = new HashMap<>();

        //dateType:日期类型 1:年 2:月 3周: 4:天
        //measureType:1:血糖 2:血压 3:血氧 4:体温 5:尿酸 6:总胆固醇 7:人体成份

        float newstSSY = 0;
        float newstSHZHY = 0;
        float newstPulse = 0;

        if (reportData.resultData.chartList != null) {


            for (int i = 0; i < reportData.resultData.chartList.size(); i++) {
                BP_R.ResultDataBean.ChartListBean data = reportData.resultData.chartList.get(i);


                if (data.pulseAvg == 0 || data.systolicAvg == 0 || data.diastolicAvg == 0) {
                    continue;
                }
                String timeParam = "";
                String disTimeParam = "";
                if (reportData.resultData.dateType == 1) {
                    Date date = TimeUtils.parseFormatter91Time(data.timeParam);
                    timeParam = CommonUtils.getMonths(NumUtils.string2Int(TimeUtils.formatTime12(date.getTime())));
                    disTimeParam=timeParam;
                } else if (reportData.resultData.dateType == 2) {
                    timeParam = String.format(getString_(R.string.Week_int),data.timeParam);
                    disTimeParam=timeParam;
                } else if (reportData.resultData.dateType == 3) {
                    Date date2 = TimeUtils.parseFormatter3Time(data.timeParam);
                    timeParam = TimeUtils.formatTime11(date2.getTime());//月-日
                    disTimeParam=timeParam;
                } else if (reportData.resultData.dateType == 4) {
                    Date date3 = TimeUtils.parseFormatter1Time(data.timeParam);
                    timeParam = TimeUtils.formatTime13(date3.getTime());//时分
                    disTimeParam=TimeUtils.formatTime4(date3.getTime());//时分
                }

                newstSSY = data.systolicAvg;
                newstSHZHY = data.diastolicAvg;
                newstPulse = data.pulseAvg;

                oneXValue.add(timeParam);
                disXValue.add(disTimeParam);

                ssyValue.put(timeParam, (float) data.systolicAvg);
                shzhyValue.put(timeParam, (float) data.diastolicAvg);

                xPointColor.add(levelColors[BpmManager.getMesureResultLevel(data.systolicAvg, data.diastolicAvg)]);
                xPointColor2.add(levelColors[1]);

                maiboValue.put(timeParam, (float) data.pulseAvg);


            }

        }


        if (reportData.resultData.summaryList != null && reportData.resultData.summaryList.size() > 0) {
            setCb8Title(shsy_cb8, 2, NumUtils.disIntZero(reportData.resultData.summaryList.get(0).systolicMin,N2));
            setCb8Title(shsy_cb8, 3, NumUtils.disIntZero(reportData.resultData.summaryList.get(0).systolicMax,N2));
            setCb8Title(shsy_cb8, 4, NumUtils.disIntZero(reportData.resultData.summaryList.get(0).systolicAvg,N2));
            setCb8SrcRes(shsy_cb8, EHCCommonUtils.getDisplaySrcRes(reportData.resultData.summaryList.get(0).systolicAvg, newstSSY));

            setCb8Title(shzhy_cb8, 2, NumUtils.disIntZero(reportData.resultData.summaryList.get(0).diastolicMin,N2));
            setCb8Title(shzhy_cb8, 3, NumUtils.disIntZero(reportData.resultData.summaryList.get(0).diastolicMax ,N2));
            setCb8Title(shzhy_cb8, 4, NumUtils.disIntZero(reportData.resultData.summaryList.get(0).diastolicAvg ,N2));
            setCb8SrcRes(shzhy_cb8, EHCCommonUtils.getDisplaySrcRes(reportData.resultData.summaryList.get(0).diastolicAvg, newstSHZHY));

            setCb8Title(maibo_cb8, 2, NumUtils.disIntZero(reportData.resultData.summaryList.get(0).pulseMin ,N2));
            setCb8Title(maibo_cb8, 3, NumUtils.disIntZero(reportData.resultData.summaryList.get(0).pulseMax ,N2));
            setCb8Title(maibo_cb8, 4, NumUtils.disIntZero(reportData.resultData.summaryList.get(0).pulseAvg ,N2));
            setCb8SrcRes(maibo_cb8, EHCCommonUtils.getDisplaySrcRes(reportData.resultData.summaryList.get(0).pulseAvg, newstPulse));
        }


        if (trend_xy_ssy_ch3 != null) {
            trend_xy_ssy_ch3.setxPointColor(xPointColor);
            trend_xy_ssy_ch3.setdisXValue(disXValue);
            trend_xy_ssy_ch3.setValue(ssyValue, oneXValue, xyYValue);
            trend_xy_ssy_ch3.src2end();
        }
        if (trend_xy_shzhy_ch3 != null) {
            trend_xy_shzhy_ch3.setxPointColor(xPointColor);
            trend_xy_shzhy_ch3.setdisXValue(disXValue);
            trend_xy_shzhy_ch3.setValue(shzhyValue, oneXValue, xyYValue);
            trend_xy_shzhy_ch3.src2end();
        }
        if (trend_xy_maibo_ch3 != null) {
            trend_xy_maibo_ch3.setxPointColor(xPointColor2);
            trend_xy_maibo_ch3.setdisXValue(disXValue);
            trend_xy_maibo_ch3.setValue(maiboValue, oneXValue, xyYValue);
            trend_xy_maibo_ch3.src2end();
        }

    }

    public static void setCb8Title(CombinView8 combinView8, int index, String title) {
        if (combinView8 != null) {
            combinView8.setTitle(index, title);
        }
    }

    public static void setCb8SrcRes(CombinView8 combinView8, int srcRes) {
        if (combinView8 != null) {
            combinView8.setImaSrc(srcRes);
        }
    }


}
